int16_t col[]={
0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x3F,0x00,0x00,0x00,0x00,0x00,
0x00,0xD8,0xEA,0x01,0x00,0x00,0x00,0x00,0x00,0xBA,0xDA,0x02,0x00,0x00,0x00,0x00,
0x00,0x2E,0x80,0x03,0x00,0x00,0x00,0x00,0x00,0x17,0x40,0x07,0x40,0x00,0x00,0x00,
0x80,0x03,0x00,0x0D,0xC0,0x01,0x00,0x00,0x00,0x03,0x00,0x1E,0xC0,0x01,0x00,0x00,
0xC0,0x71,0x1C,0x94,0x61,0x20,0x00,0x00,0x40,0x71,0x0C,0x8C,0xC3,0x70,0x00,0x00,
0x80,0x81,0x09,0x18,0xE3,0x70,0x00,0x00,0x60,0xF1,0x1F,0x1A,0xE3,0x70,0x00,0x00,
0xC0,0xB1,0x0F,0xAC,0x63,0x10,0x00,0x00,0x80,0x72,0x18,0x0A,0xE3,0x38,0x80,0x00,
0x80,0xA1,0x0C,0x8C,0x62,0x18,0xE0,0x03,0x00,0xEB,0x0F,0x0F,0xE3,0x90,0xEA,0x07,
0x40,0x07,0x05,0x05,0x02,0x80,0xFF,0x07,0x00,0x5E,0xF0,0x07,0x40,0xC0,0xFF,0x0F,
0x00,0xB8,0xEF,0x07,0x00,0xE0,0x2B,0x02,0x00,0xF4,0xFF,0x0F,0x64,0xC6,0x42,0x04,
0x00,0x80,0x2F,0x0E,0x67,0xC6,0x01,0x00,0x00,0xC0,0x01,0x3C,0x06,0xC4,0x01,0x00,
0x00,0xC0,0x01,0x18,0x00,0xC0,0x00,0x00,0x00,0xC0,0x01,0x38,0x00,0xC8,0x01,0x00,
0x00,0xA0,0x01,0x78,0x00,0xE0,0x00,0x00,0x00,0xE0,0x00,0x70,0x00,0xC0,0x01,0x00,
0x00,0xE0,0x00,0xE0,0x00,0xC0,0x00,0x00,0x00,0xB0,0x50,0xC0,0x00,0xC0,0x01,0x00,
0x00,0x71,0x10,0xF0,0x00,0xE0,0x00,0x00,0x08,0x30,0x1C,0xC0,0x00,0xC0,0x00,0x00,
0x00,0x78,0x1C,0xC0,0x02,0xE0,0x01,0x00,0x00,0x58,0x1C,0xC0,0x01,0x40,0x01,0x00,
0x00,0x1F,0x87,0xC0,0x01,0xE0,0x00,0x00,0x80,0x86,0x07,0xE0,0x00,0xE0,0x00,0x00,
0x80,0x8F,0x05,0xC0,0x01,0xE0,0x00,0x00,0xD0,0xD1,0x03,0xC0,0x01,0xC0,0x00,0x00,
0xE0,0xF7,0x00,0xC0,0x00,0xF0,0x00,0x00,0xE0,0x7F,0x05,0xF0,0x00,0x60,0x00,0x00,
0xC0,0x9F,0x00,0xE0,0x52,0xE2,0x01,0x00,0x40,0x03,0xA0,0xFA,0xD6,0x6E,0x00,0x00,
0x80,0x01,0xFF,0xFF,0xFF,0xFD,0x00,0x00,0x80,0xA3,0xFF,0xEF,0xBF,0x7F,0x00,0x00,
0x80,0xE3,0x57,0x12,0x00,0xE0,0x01,0x00,0x40,0x63,0x47,0x00,0x40,0x62,0x00,0x00,
0xC0,0xF2,0xDF,0x37,0xD0,0xFF,0x00,0x00,0x80,0x31,0xFF,0x1F,0xE0,0xFF,0x7B,0x0B,
0xC0,0xF3,0x76,0x55,0x80,0xFA,0xEF,0x07,0x80,0x31,0x8E,0x04,0x00,0xD8,0xBE,0x0F,
0xC0,0x79,0x0C,0x00,0x00,0xB8,0x24,0x00,0x80,0x1D,0x1E,0x00,0x00,0x18,0x00,0x04,
0xC0,0x3F,0x0C,0x00,0x00,0x30,0x00,0x00,0x00,0x17,0x0C,0x00,0x00,0x3C,0x00,0x00,
0x40,0x25,0x1E,0x00,0x00,0x3C,0x00,0x00,0x00,0x04,0x78,0x80,0x00,0x0A,0x00,0x00,
0x00,0x00,0xF8,0x0D,0x80,0x1F,0x00,0x00,0x00,0x00,0xF0,0x1F,0xE0,0x0F,0x00,0x00,
0x00,0x00,0x40,0x0D,0xE0,0x01,0x00,0x00,0x00,0x00,0x00,0x1A,0x50,0x00,0x00,0x00,
0x00,0x00,0x00,0x3C,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0xE0,0x00,0x00,0x00};

#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Adafruit_ssd1306syp.h>
#define SDA_PIN 5
#define SCL_PIN 4
Adafruit_ssd1306syp display(SDA_PIN, SCL_PIN);
HTTPClient http;
String GetUrl;
String response;
WiFiUDP ntpUDP;
int dark = 0;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 3600, 60000);

void SmartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for Smartconfig...");
  WiFi.beginSmartConfig();
  while (1)
  {
    Serial.print(".");
    delay(500);                   // wait for a second
    if (WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;
    }
  }
}
bool AutoConfig()
{
  WiFi.begin();
  //如果觉得时间太长可改
  for (int i = 0; i < 10; i++)
  {
    int wstatus = WiFi.status();
    if (wstatus == WL_CONNECTED)
    {
      Serial.println("WIFI SmartConfig Success");
      Serial.printf("SSID:%s", WiFi.SSID().c_str());
      Serial.printf(", PSW:%s\r\n", WiFi.psk().c_str());
      Serial.print("LocalIP:");
      Serial.print(WiFi.localIP());
      Serial.print(" ,GateIP:");
      Serial.println(WiFi.gatewayIP());

      return true;
    }
    else
    {
      Serial.print("WIFI AutoConfig Waiting......");
      Serial.println(wstatus);

      display.clear();
      display.setTextSize(2);
      display.setCursor(10, 10);
      display.println("AutoConfig Waiting");
      display.update();
      delay(1000);
    }
  }
  Serial.println("WIFI AutoConfig Faild!" );


  display.clear();
  display.setTextSize(2);
  display.setCursor(10, 10);
  display.println("EspTouch");
  display.update();
  delay(500);
  return false;
}

void timedisplay()
{
  display.clear();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(5, 0);
  display.print("IP:");
  display.setCursor(25, 0);
  display.println(WiFi.localIP());
  display.setTextSize(2);
  display.setCursor(10, 20);
  display.print(response.substring(13, 23));
  display.setCursor(20, 40);
  display.print(timeClient.getFormattedTime());
  display.update();


}


void sleepdisplay()
{
  display.clear();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(10, 20);
  display.print("Time To");
  display.setCursor(30, 40);
  display.print("Sleep~~");
  display.update();


}
void setup() {
  display.initialize();
  dark = 0;
  Serial.begin(115200);
  delay(100);
  display.clear();
display.drawBitmap(col,0,0,60,60,1);



  
  display.clear();
  display.setTextSize(2);
  display.setCursor(17, 10);
  display.println("Tingting");
  display.setTextSize(1);
  display.setCursor(20, 40);
  display.println("Project::BOX");
  display.update();
  delay(5000);

  if (!AutoConfig())
  {

    display.clear();
    display.setTextSize(2);
    display.setCursor(10, 10);
    display.println("SmartConfig");
    display.update();
    SmartConfig();
  }


  display.clear();
  display.setTextSize(2);
  display.setCursor(10, 10);
  display.println("CONNECTED");
  display.update();
  delay(1000);

  GetUrl = "http://quan.suning.com/getSysTime.do";

  http.setTimeout(5000);

  http.begin(GetUrl);
}

int counter = 0;
int hours;
void loop()
{
  if (counter >= 1000)
    counter = 0;
  timeClient.update();
  hours = timeClient.getHours() + 7;
  if ((hours >= 23 || hours <= 6) && dark == 0)
  {
    display.initializedark();
    dark = 1;
  }
  else if ((hours > 6 && hours < 23) && dark == 1)
  {
    display.initialize();
    dark = 0;
  }
  if (counter == 0)
  {
    int httpCode = http.GET();

    if (httpCode > 0) {
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      if (httpCode == HTTP_CODE_OK) {
        response = http.getString();
        Serial.println(response);
      }
    }

    http.end();

  }

  //Serial.println(hours);
  if (hours == 0 && timeClient.zhengHours() == 0  )
  {
    for (int i = 0; i <= 10; i++)
    { timedisplay();
      delay(200);
      sleepdisplay();
      delay(200);
    }
  }
  else if (timeClient.zhengHours() == 0)
  {
    for (int i = 0; i <= 10; i++)
    {
      timedisplay();
      delay(200);
      display.clear();
      display.update();
      delay(200);
    }
  }
  else
    timedisplay();
  counter++;
  delay(200);
}
